/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontend;

import hibernate.impl.UserDAOImpl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modelmapping.User;
import util.PageNotFoundException;
import util.PentaUtil;
import validator.ValidatorError;

/**
 *
 * @author Valentina
 */
public class UserPrepareUpdateServlet extends HttpServlet {

  
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        List myErrors = new ArrayList();
        HttpSession session = request.getSession();
        String strUserId = request.getParameter("id");
        myErrors.clear();

        try {
            if (strUserId != null) {
                Long userId = Long.parseLong(strUserId);

                UserDAOImpl userDAOImpl = new UserDAOImpl();
                User user = (User) userDAOImpl.retrieve(userId);

                if (user == null) {
                    throw new PageNotFoundException("User not Found with id =" + userId);
                }
                request.setAttribute("user", user);
                session.setAttribute("lastaction", "UserPrepareUpdateServlet?id="+userId);
            }else{
                session.setAttribute("lastaction", "UserPrepareUpdateServlet");
            }

            PentaUtil.getGroupLits(request);
            PentaUtil.RequestUtil(request, response, "WEB-INF/frontend/UserUpdate.jsp");

        } catch (NumberFormatException nfe) {
            myErrors.add(new ValidatorError("numberFormatError", "Number Format Error"));
            PentaUtil.setError(request, response, myErrors);
        } catch (PageNotFoundException unfe) {
            myErrors.add(new ValidatorError("userNotFound", unfe.getMessage()));
            PentaUtil.setError(request, response,myErrors);
        } catch (Exception e) {
            myErrors.add(new ValidatorError("systemError", "System Error"));
            PentaUtil.setError(request, response,myErrors);
        }

    }
  
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}